package com.farewell.controller;

import com.farewell.pojo.Product;
import com.farewell.service.ProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.PropertyValuesEditor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author Farewell
 * @date 2021/3/23
 */
@Controller
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        CustomDateEditor dateEditor = new CustomDateEditor(sdf, true);
        binder.registerCustomEditor(Date.class, dateEditor);
    }

    @RequestMapping("/updatePage.do")
    public ModelAndView updatePage(@RequestParam("id") String id) {
        ModelAndView mv = new ModelAndView();
        Product product = productService.findOne(id);
        if (product != null) {
            mv.addObject("product", product);
            mv.setViewName("product-add");
            return mv;
        }else {
            return null;
        }
    }

    @RequestMapping("/findAll.do")
    public ModelAndView findPageAll(@RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum) {
        ModelAndView mv = new ModelAndView();
        PageHelper.startPage(pageNum, 10);
        List<Product> productList = productService.findAll();
        PageInfo<Product> pageInfo = new PageInfo<Product>(productList);
        mv.addObject("pageInfo", pageInfo);
        mv.setViewName("product-list");
        return mv;
    }

    @RequestMapping("/save.do")
    public String save(Product product, @RequestParam(defaultValue = "", value = "id") String id) {
        try {
            if (!id.isEmpty()) {
                product.setId(id);
                productService.update(product);
            }else {
                product.setId(null);
                productService.save(product);
            }
            return "redirect:findAll.do";
        } catch (Exception e) {
            e.printStackTrace();
            return "操作失败";
        }
    }

    @RequestMapping("/delete.do")
    public String delete(@RequestParam(value="ids[]") String[] ids) {
        try {
            for (String s : ids) {
                productService.delete(s);
            }
            return "操作成功";
        } catch (Exception e) {
            e.printStackTrace();
            return "删除失败";
        }
    }

    @RequestMapping("/updateStatus.do")
    public String updateStatus(@RequestParam(value="ids[]") String[] ids) {
        try {
            for (String id : ids) {
                productService.updateStatus(id);
            }
            return "操作成功";
        } catch (Exception e) {
            e.printStackTrace();
            return "更新失败";
        }
    }
}
